//
//  CYloginRegisterViewController.m
//  聪颖不聪颖
//
//  Created by 葛聪颖 on 15/9/27.
//  Copyright © 2015年 gecongying. All rights reserved.
//

#import "CYloginRegisterViewController.h"
#import "UIView+CYExtenssion.h"
//#import "UIColor+UIColor_Hex.h"
#import "UIImageView+WebCache.h"

@interface CYloginRegisterViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leadingSpace;
//登录按钮
@property (weak, nonatomic) IBOutlet UIButton *btnLogin;
@property (weak, nonatomic) IBOutlet UITextField *tfPhone;
@property (weak, nonatomic) IBOutlet UITextField *tfPwd;
//登录框背景
@property (weak, nonatomic) IBOutlet UIImageView *phoneBg;
//界面背景
@property (weak, nonatomic) IBOutlet UIImageView *loginBg;


@end

@implementation CYloginRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    self.loginButton.layer.cornerRadius = 5;
    //    self.loginButton.layer.masksToBounds = YES;
    //    [self.loginButton setValue:@5 forKeyPath:@"layer.cornerRadius"];
    //    [self.loginButton setValue:@YES forKeyPath:@"layer.masksToBounds"];
    //    self.loginButton.clipsToBounds = YES;
    
    NSDictionary *dict = @{@"logoId":LOGOID};
    NSString *urlString = [NSString stringWithFormat:@"%@%@",uHostURL,getAppLogin];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:urlString parameters:dict success:^(id obj) {
        NSDictionary *dic = (NSDictionary *)obj;
        NSString *errorcodeString = dic[@"errorcode"];
        if ([dic[@"data"] isEqual:[NSNull null]]) {
            return;
        }
        DLog(@"LoginAction:%@",obj);
        if (![errorcodeString isEqualToString:@"9999"]) {
            return;
        }
        [self loginSetAction:obj];
    } fail:^(NSError *error) {
        
        
        
    }];
//    [self LoginAction:nil];
//    [self LoginAction:nil];
}
- (void)viewWillAppear:(BOOL)animated {
     [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (IBAction)LoginAction:(id)sender {
    
    if (_tfPwd.text.length == 0 || _tfPhone.text.length == 0) {
        [EPProgressHUD showErrorWithStatus:@"用户名密码不能为空"];
        return;
    }
    NSDictionary *dict = @{@"logoId":LOGOID,
                           @"username":_tfPhone.text,
                           @"password":_tfPwd.text};
    NSString *urlString = [NSString stringWithFormat:@"%@%@",uHostURL,getAppLogin];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:@"http://192.168.20.51:9999/userRest/login" parameters:dict success:^(id obj) {
        NSDictionary *dic = (NSDictionary *)obj;
        NSString *errorcodeString = dic[@"errorcode"];
        if ([dic[@"data"] isEqual:[NSNull null]]) {
            return;
        }
        DLog(@"%@",obj);
        if (![errorcodeString isEqualToString:@"9999"]) {
            return;
        }
        [self loginSetAction:obj];
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark -设置登录界面
- (void)loginSetAction:(NSDictionary *)dictionary {
    if (dictionary==nil || ![dictionary isKindOfClass:[NSDictionary class]]) {
        return;
    }
    //背景图片
    NSString *bgPicture = dictionary[@"data"][@"bgPicture"];
    if (bgPicture.length == 0) {
        
    }
    
    //登录按钮颜色
    UIColor *BtBgColor = [UIColor colorWithHexString:dictionary[@"loginBtBgColor"]];
    //登录按钮文字颜色
    UIColor *loginBtFontColor = [UIColor colorWithHexString:dictionary[@"loginBtFontColor"]];
    //输入框文字颜色
    UIColor *inputFoLntColor = [UIColor colorWithHexString:dictionary[@"inputFoLntColor"]];
     //背景图片
    NSString *url = [NSString stringWithFormat:@"%@%@",uHostURL_IMAGE,bgPicture];
    [_loginBg sd_setImageWithURL:URLWithString(url) placeholderImage:ImageNamed(@"login_register_background")];
    //登录按钮颜色
    if([dictionary[@"loginBtBgColor"] isEqualToString:@""]) {
        [_btnLogin setTintColor:[UIColor blueColor]];
    }else {
        [_btnLogin setTintColor:BtBgColor];
    }
    
    if([dictionary[@"loginBtFontColor"] isEqualToString:@""]) {
        [_btnLogin setTitleColor:loginBtFontColor forState:UIControlStateNormal];
    }else {
        [_btnLogin setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
    //登录按钮文字颜色
    if([dictionary[@"inputFoLntColor"] isEqualToString:@""]) {
        [_tfPhone setTextColor:inputFoLntColor];
        [_tfPwd setTextColor:inputFoLntColor];
    }else {
        [_tfPhone setTextColor:[UIColor blackColor]];
        [_tfPwd setTextColor:[UIColor blackColor]];
    }
   

}

- (IBAction)close
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (IBAction)loginOrRegister:(UIButton *)button
{
    // 修改约束
    if (self.leadingSpace.constant == 0) {
        self.leadingSpace.constant = - self.view.width;
//        [button setTitle:@"已有账号？" forState:UIControlStateNormal];
        button.selected = YES;
    }else
    {
        self.leadingSpace.constant = 0;
//        [button setTitle:@"注册账号" forState:UIControlStateNormal];
        button.selected = NO;
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}


@end
